package com.lqm.web.controller.home;

import com.lqm.entity.home.Comments;
import com.lqm.entity.home.HomeUser;
import com.lqm.entity.home.Project;
import com.lqm.service.home.CommentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("home")
public class CommentsController {

    @Autowired
    CommentsService commentsService;

    @RequestMapping("findAllByProject")
    public Map<String ,Object> findAllByProject(Project project,int limit,int page) {
        HashMap<String, Object> map = new HashMap<>(0);
        System.out.println("--------------------"+project.getProjectId());
        List<Comments> all = commentsService.findAllByProject(project,limit, page);
        System.out.println(all);
        for (Comments comments : all) {
            comments.setProject(null);
            HomeUser user = comments.getHomeUser();
            user.setProjects(null);
            comments.setHomeUser(user);
        }
        map.put("data",all);
        map.put("code",0);

        return map;
    }


    /**
     * 添加一个评论
     * @param comments
     * @return
     */
    @RequestMapping("addComments")
    public Map<String ,Object> addComments(Comments comments, HttpSession session){
        HashMap<String, Object> map = new HashMap<>(0);
        HomeUser homeUser = (HomeUser) session.getAttribute("home_user");
        comments.setHomeUser(homeUser);
//        System.out.println(comments);
        Comments addComments = commentsService.addComments(comments);
        if (addComments!=null){
            map.put("code",0);
            map.put("msg","留言成功");
        }else {
            map.put("code",1);
            map.put("msg","留言失败");
        }
        return map;
    }
}
